/*
 * @Author: modefy
 * @Date: 2021-12-06 10:13:57
 * @LastEditors: wangdan
 * @LastEditTime: 2023-05-11 09:38:21
 * @Description: Description
 */
import { RouteRecordRaw, createRouter, createWebHashHistory } from 'vue-router'
import Layout from 'components/layouts/index.vue'

export const routes: RouteRecordRaw[] = [
  // / 重定向至登录页
  {
    path: '/',
    redirect: '/login',
    meta: { isHidden: true }
  },
  {
    path: '/login',
    name: 'login',
    meta: { isHidden: true },
    component: () => import('@/views/login/login.vue')
  },
  {
    path: '/home',
    component: Layout,
    redirect: '/home/home',
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('@/views/home.vue'),
        meta: { title: '首页', icon: 'House' }
      }
    ]
  },
  {
    path: '/books',
    name: 'books',
    redirect: '/books/book',
    component: Layout,
    meta: { title: '图书管理', icon: 'Reading' },
    children: [
      {
        path: 'book',
        name: 'book',
        component: () => import('views/books/index.vue'),
        meta: { title: '图书列表' }
      }
      // {
      //   path: 'kind',
      //   name: 'kind',
      //   component: () => import('views/books/index.vue'),
      //   meta: { title: '图书分类' }
      // }
    ]
  },
  {
    path: '/kld',
    name: 'kld',
    redirect: '/kld/know',
    component: Layout,
    meta: { title: '技术知识', icon: 'Collection' },
    children: [
      {
        path: 'know',
        name: 'know',
        component: () => import('views/kld/knowledge.vue'),
        meta: { title: '知识列表' }
      },
      {
        path: 'addknow',
        name: 'addknow',
        component: () => import('views/kld/addOrUpdate.vue'),
        meta: { title: '添加知识', isHidden: true }
      },
      {
        path: 'sort',
        name: 'sort',
        component: () => import('@/views/kld/sort/sort.vue'),
        meta: { title: '分类' }
      },
      {
        path: 'addsort',
        name: 'addsort',
        component: () => import('views/kld/sort/addOrUpdate.vue'),
        meta: { title: '添加分类', isHidden: true }
      },
      {
        path: 'tinymce',
        name: 'tinymce',
        component: () => import('@/views/kld/tinymce/tinymce.vue'),
        meta: { title: '富文本编辑器' }
      }
    ]
  },
  {
    path: '/daily',
    name: 'daily',
    redirect: '/daily/notes',
    component: Layout,
    meta: { title: '日常管理', icon: 'Collection' },
    children: [
      {
        path: 'notes',
        name: 'notes',
        component: () => import('views/daily/index.vue'),
        meta: { title: '心情随笔' }
      }
    ]
  },
  {
    path: '/commodity',
    name: 'commodity',
    redirect: '/commodity/clothes',
    component: Layout,
    meta: { title: '商品管理', icon: 'Collection' },
    children: [
      {
        path: 'clothes',
        name: 'clothes',
        component: () => import('views/commodity/index.vue'),
        meta: { title: '服装列表' }
      }
    ]
  },
  {
    path: '/travel',
    name: 'travel',
    redirect: '/travel/scenery',
    component: Layout,
    meta: { title: '旅游管理', icon: 'Collection' },
    children: [
      {
        path: 'scenery',
        name: 'scenery',
        component: () => import('views/travel/index.vue'),
        meta: { title: '景点列表' }
      }
    ]
  }
]

const Router = createRouter({
  history: createWebHashHistory(),
  routes
})

Router.beforeEach((to, from, next) => {
  // 如果跳转页面不是登录页，并且本地存储没有token，就跳转到登录页，否则放行
  if (to.path !== '/login' && !localStorage.getItem('token')) {
    next('/login')
  } else {
    next()
  }
})

export default Router
